package com.example.sunsg.okhttptest.customview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.breadtrip.R;

/**
 * Created by sunsg on 2017/10/24.
 */

public class LotteryCustomView extends View{
    /**
     * 抽奖的文字
     */
    private String[] mText = new String[]{"苹果手机", "小米手机", "乐视手机", "锤子手机", "华为手机", "恭喜发财"};
    /**
     * 每个盘块的颜色
     */
    private int[] mColors = new int[]{0xFFFFC300, 0xFFF17E01, 0xFFFFC300,
            0xFFF17E01, 0xFFFFC300, 0xFFF17E01};
    /**
     * 与文字对应的图片
     */
    private int[] mResourceIds = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};

    /**
     * 与文字对应图片的bitmap数组
     */
    private Bitmap[] mBitmap;

    /**
     * 盘块的个数
     */
    private int mItemCount = 6;

    /**
     * 绘制盘块的范围
     */
    private RectF mRange = new RectF();

    /**
     * 圆的直径
     */
    private int mRadius;

    /**
     * 绘制盘快的画笔
     */
    private Paint mArcPaint;

    /**
     * 绘制文字的画笔
     */
    private Paint mTextPaint;
    /**
     * 滚动的速度
     */
    private double mSpeed;
    /**
     * 起始角度
     */
    private volatile float mStartAngle = -30;

    /**
     * 圆弧角度
     */
    private float mSweepAngle;

    /**
     * 画文字的 path
     */
    private Path mTextPath;

    /**
     * 文字的大小
     */
    private float mTextSize = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics());

    private int  mCenter;
    public LotteryCustomView(Context context) {
        this(context,null);
    }

    public LotteryCustomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public LotteryCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        //设置可获得焦点
        setFocusable(true);
        setFocusableInTouchMode(true);
        //设置常亮
        this.setKeepScreenOn(true);

        // 初始化绘制圆弧的画笔
        mArcPaint = new Paint();
        mArcPaint.setAntiAlias(true);
        mArcPaint.setDither(true);

        // 初始化绘制文字的画笔
        mTextPaint = new Paint();
        mTextPaint.setColor(0xffffffff);
        mTextPaint.setTextSize(mTextSize);

        mSweepAngle = 360 / mItemCount;

        mTextPath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int w = MeasureSpec.getSize(widthMeasureSpec);
        int h = MeasureSpec.getSize(heightMeasureSpec);
        int min = Math.min(w,h) - getPaddingLeft() - getPaddingRight();
        mRadius = min;
        mRange = new RectF(getPaddingLeft(),getPaddingTop(),mRadius +getPaddingLeft(),mRadius + getPaddingTop());
        mCenter = w / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        realDraw(canvas);
    }

    private void realDraw(Canvas canvas){
        for(int i = 0;i<mItemCount;i++){
            mArcPaint.setColor(mColors[i]);
            canvas.drawArc(mRange,mStartAngle,mSweepAngle,true,mArcPaint);
            drawText(canvas,mStartAngle,mSweepAngle,mText[i]);
            drawIcon(canvas,mStartAngle,mResourceIds[i]);
            mStartAngle += mSweepAngle;
        }
        mStartAngle += 90;
    }

    private void drawText(Canvas canvas,float startAngle,float sweepAngle,String string){
        mTextPath.addArc(mRange,startAngle,sweepAngle);
        float textWidth = mTextPaint.measureText(string);
        float hOffset = (float) (Math.PI * mRadius / mItemCount /2 - textWidth/2);
        float vOffset = mRadius / 2 / 6;
        canvas.drawTextOnPath(string,mTextPath,hOffset,vOffset,mTextPaint);
        mTextPath.reset();
        Log.i("test","onDraw");
    }

    private void drawIcon(Canvas canvas,float startAngle,int  iconResourceId){
        // 设置图片的宽度为直径的1/8
        int imgWidth = mRadius / 8;

        float angle = (float) ((30 + startAngle) * (Math.PI / 180));

        int x = (int) (mCenter + mRadius / 2 / 2 * Math.cos(angle));
        int y = (int) (mCenter + mRadius / 2 / 2 * Math.sin(angle));

        // 确定绘制图片的位置
        Rect rect = new Rect(x - imgWidth / 2, y - imgWidth / 2, x + imgWidth
                / 2, y + imgWidth / 2);

        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),iconResourceId), null, rect, null);
    }

}
